//
//  GameView.m
//  AnotherFire
//
//  Created by pcwiz on 23/06/09.
//  Copyright 2009 leowiz. All rights reserved.
//

#import "GameView.h"


@implementation GameView
@synthesize parent;

- (id)initWithFrame:(CGRect)frame {
	printf("Game view initialized.\n");
    if (self = [super initWithFrame:frame]) {
        // Initialization code
    }
	
    return self;
}

- (void) newParent: (GameContorllerDelegate *)_parent
{
	printf("Setting parent...");
	self.parent = _parent;
	printf("Parent set.\n");
}

- (void) touchesEnded:(NSSet *) touches withEvent: (UIEvent *) event
{
	printf("A touch ended!\n");
	UITouch *t = [touches anyObject];
	CGPoint point = [t locationInView: self];
	[self.parent touchOccured: point];
}


- (void)drawRect:(CGRect)rect {
	printf("Drawing the rect with.");
	[super drawRect:rect];
	GameLogic *gl = [self.parent gl];
	
	// Drawing code
	int col_width = rect.size.width / [gl width];
	int row_height = (rect.size.height - 80) / [gl height];
	
	
    CGContextRef gc = UIGraphicsGetCurrentContext();
	
	CGContextTranslateCTM(gc, 0, rect.size.height);
	CGContextScaleCTM(gc, 1.0, -1.0);
	
	CGContextSetRGBFillColor(gc, 1, 0.75, 0.75, 1);

	
	for (int i=0; i < gl.height; ++i) {
		NSMutableArray *cur_row = [gl.board objectAtIndex:i];
		for (int j=0; j < gl.width; ++j) 
		{
			Side *s = [cur_row objectAtIndex:j];
			CGContextSetRGBFillColor(gc, 0, 0, 0, 1);
			
			if (1 == [gl.sides indexOfObject: s])
			{
				CGContextSetRGBFillColor(gc, 1, 0, 0, 1);
			}
			else if (0 == [gl.sides indexOfObject: s])
			{
				CGContextSetRGBFillColor(gc, 0, 1, 0, 1);
			}
			CGContextFillRect(gc, CGRectMake(col_width * j, row_height * i, col_width, row_height));
		}
	}
	
	//Draw board:
	CGContextSetRGBFillColor(gc, 1, 1, 1, 1);
	
	//Draw row lines:
	int cur_row_start = 0;
	for (int i = 0; i < [gl height]; ++i) {
		CGContextFillRect(gc, CGRectMake(0, cur_row_start, rect.size.width, 1));
		cur_row_start += row_height;
	}
	
	//Draw column lines:
	int cur_col_start = 0;
	for (int i = 0; i < [gl width]; ++i) {
		CGContextFillRect(gc, CGRectMake(cur_col_start, 0, 1, rect.size.height - 80));
		cur_col_start += col_width;
	}
	//Last line:
	CGContextFillRect(gc, CGRectMake(cur_col_start - 1, 0, 1, rect.size.height - 80));	
	
	//Draw current player:
	if (1 == gl.cur_side)
	{
		CGContextSetRGBFillColor(gc, 1, 0, 0, 1);
	}
	else if (0 == gl.cur_side)
	{
		CGContextSetRGBFillColor(gc, 0, 1, 0, 1);
	}
	
	CGContextFillRect(gc, CGRectMake(10, 415, 30, 30));

	NSString *cur_player = @"Current player is:";
	cur_player = [cur_player stringByAppendingString: [[gl.sides objectAtIndex: gl.cur_side] side_name]];
    char text[101];
	[cur_player getCString:text maxLength:100 encoding:NSUTF8StringEncoding];
	
	CGContextSelectFont(gc, "Helvetica", 17, kCGEncodingMacRoman);
	CGContextSetTextDrawingMode(gc, kCGTextFill);
	CGContextSetRGBFillColor(gc, 255, 0, 255, 1);
	CGContextShowTextAtPoint(gc, 50, 430, text, strlen(text));
}




- (void)dealloc {
	[self.parent release];
    [super dealloc];
}


@end
